home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.2 Applications 1996 May
/
SGI IRIX 6.2 Applications 1996 May.iso
/
dist
/
impr_dev.idb
/
usr
/
impressario
/
src
/
libstiff
/
stiff.c.z
/
stiff.c
Wrap
C/C++ Source or Header
|
1996-05-06
|
6KB
|
286 lines
/*
* stiff.c
*
* stream tiff stuff - mostly public wrappers around the tag stuff;
* the goal being to insulate the programmer from icky tag stuff as
* much as possible.
*
* Copyright 1993, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stiff.h>
#include <bstring.h>
#include "stiffp.h"
/*
* int
* STReadImageHeader(STStream *st, STImageHeader *hd)
*
* Description:
* Reads the next image header from the stream
*
* Parameters:
* st tiff stream
* hd data structure to receive header information
*
* Returns:
* 0 if successful, -1 if error
*/
int
STReadImageHeader(STStream *st, STImageHeader *hd)
{
STTagType type;
unsigned long len, *doffsetp;
STStream *newStream;
fd_set fds;
struct timeval tv;
_STFreeTags(st->tags);
/*
* If read_tags returns NULL we will assume that there were
* no tags to read
*/
if ((st->tags = _STReadTags(st)) == NULL) {
/*
* If it's anything other than an end of stream, forget it.
*/
if (STerrno != STEEOF) {
return -1;
}
/*
* This allows multiple stiff files to be concatenated
* together and read in by applications. We call STOpen to
* see if there's another stiff "file" beyond this one, and if
* so, we just keep going.
*/
FD_ZERO(&fds);
FD_SET(st->fd, &fds);
timerclear(&tv);
/*
* Don't block on a pipe
*/
if (select(st->fd + 1, &fds, NULL, NULL, &tv) != 1 ||
!FD_ISSET(st->fd, &fds)) {
return -1;
}
newStream = STOpen(st->fd, ST_READ);
if (newStream == NULL ||
(newStream->tags = _STReadTags(newStream)) == NULL) {
if (newStream) {
free(newStream);
}
STerrno = STEEOF;
return -1;
}
*st = *newStream;
free(newStream);
}
/*
* Point to image data. Use the first one if there are more than
* one, making the possibly rash assumption that each strip
* immediately follows the one before it.
*/
if (_STGetTag(st->tags, STStripOffsets, &type, &len,
(void**)&doffsetp) < 0) {
return -1;
}
if (type != ST_TAG_TYPE_LONG) {
STerrno = STEBADTAG;
return -1;
}
if (STSkipTo(st, *doffsetp) < 0) {
return -1;
}
/*
* fill in the image header
*/
return _STUntagify(st->tags, hd);
}
/*
* int
* STWriteImageHeader(STStream *st, STImageHeader *hd, int last)
*
* Description:
* Writes an image header onto the stream
*
* Parameters:
* st tiff stream
* hd the header to write
* last set to 1 if this is the last image, set to 0 if not
*
* Returns:
* 0 if successful, -1 if error
*/
int
STWriteImageHeader(STStream *st, STImageHeader *hd, int last)
{
STTagList *tags;
int status;
tags = _STTagify(st->tags, hd);
if (!tags) {
return -1;
}
status = _STWriteTags(st, tags, last);
_STFreeTags(tags);
/*
* st->tags were merged in with tags, so now that we've freed tags
* we need to clobber st->tags.
*/
st->tags = 0;
return status;
}
/*
* int
* STAddTag(STStream *st, unsigned short tag, STTagType type,
* unsigned long len, void *val)
*
* Description:
* Add a tag to the stream. It will get written into the next
* image file directory written by STWriteImageHeader().
*
* If there already was a tag with the same "tag" value, it will
* get overwritten.
*
* Parameters:
* st tiff stream
* tag tag to write
* type type of tag
* len length of data
* val pointer to the data
*
* Returns:
* 0 if successful, -1 if error
*/
int
STAddTag(STStream *st, unsigned short tag, STTagType type,
unsigned long len, void *val)
{
STTagList *tmp;
tmp = _STInsertTag(st->tags, tag, type, len, val);
if (!tmp) {
return -1;
}
st->tags = tmp;
return 0;
}
/*
* int
* STRemoveTag(STStream *st, unsigned short tag)
*
* Description:
* Removes a tag from the tags that will be written next time
* STWriteImageHeader() is called.
*
* Parameters:
* st tiff stream
* tag tag to remove
*
* Returns:
* 0 if successful, -1 if error
*/
int
STRemoveTag(STStream *st, unsigned short tag)
{
STTagList *tmp;
if (_STDeleteTag(st->tags, tag, &tmp) == -1) {
return -1;
}
st->tags = tmp;
return 0;
}
/*
* int
* STGetTag(STStream *st, unsigned short tag, STTagType *type,
* unsigned long *len, void **val)
*
* Description:
* Get the value of a tag that has been added to st, either by
* being read from the stream or by a call to STAddTag().
*
* Parameters:
* st tiff stream
* tag tag to get
* type receives type of tag
* len receives length of tag
* valp receives tag data. If more than can fit in 4 bytes,
* this will be a pointer into memory owned by the tags
* module, so don't muck with it!!! And don't assume that
* it will be meaningful after a subsequent call to
* STReadImageHeader!!! If you want to preserve the value,
* copy it to your space.
*
* Returns:
* 0 if successful, -1 if error.
*/
int
STGetTag(STStream *st, unsigned short tag, STTagType *type,
unsigned long *len, void **valp)
{
return _STGetTag(st->tags, tag, type, len, valp);
}
/*
* int
* STPrintTags(STStream *st)
*
* Description:
* st->tags
*
* Parameters:
* st tiff stream to print tags of
*
* Returns:
* 0 if successful, -1 if error
*/
int
STPrintTags(FILE *fp, STStream *st)
{
return _STPrintTags(fp, st->tags);
}